package com.john.blog.common.utils;

import java.security.MessageDigest;
import java.util.UUID;

/**
 * key工具
 * 
 * @author JiangGengchao
 * @date 2018年2月9日
 */
public class KeyUtils {

    /**
     * 获取 UUID
     * 
     * @return
     */
    public static String getUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    /**
     * 对内容进行签名，使用MD5进行哈希
     * 
     * @param value
     * @return
     */
    public static String signByMD5(String value) {
        return encodeMD5Hex(value);
    }

    private static final String MD5 = "MD5";

    public static byte[] encodeMD5(byte[] data) {
        try {
            MessageDigest md = MessageDigest.getInstance(MD5);
            return md.digest(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static byte[] encodeMD5(String data) {
        try {
            return encodeMD5(data.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static final char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

    public static String convert2HexStr(byte[] data) {
        int j = data.length;
        char[] str = new char[j * 2];
        int k = 0;
        for (int i = 0; i < j; i++) {
            byte byte0 = data[i];
            str[(k++)] = hexDigits[(byte0 >>> 4 & 0xF)];
            str[(k++)] = hexDigits[(byte0 & 0xF)];
        }
        return new String(str);
    }

    public static String encodeMD5Hex(String data) {
        try {
            return convert2HexStr(encodeMD5(data));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
